﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using YT.DB;
using YT.Commons;
 
namespace YT.XH.MF
{
    public partial class frmmUserMtn : Form
    {

        private DBAccess mdbcon = new DBAccess(false,false );
        private string mstrRecID = string.Empty;
        private string mstrTableName = string.Empty;
        private FormStatus mFormStatus = new FormStatus();
        private ArrayList mArrayControl=new ArrayList ();
        public frmmUserMtn()
        {
            InitializeComponent();
        }
        
        
        private void initControl()
        {
            dgvListView.ConnectionString = DBAccess.ConnectionDBString;
            dgvListView.Sql = DataFunctions.GetLookUpSQL("mUser", "");
            dgvListView.TableName = "mUser";
        }
        private void SelectNextText(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
                SelectNextControl((Control)sender, true, true, true, false);
        }
        private void frmmUser_Load(object sender, EventArgs e)
        {
            this.Text = SysVar.CurrentMenuInfo.strMenuName;  
            initControl();
            mstrTableName = "mUser";
            mArrayControl.Clear();
            foreach (Control con in this.pnlDetail.Controls)
            {
                mArrayControl.Add(con);
                con.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SelectNextText); 
            }            
            mFormStatus = FormStatus.Query;
            SetButtonStatus();
        }

        private void frmmUserMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Update())
                this.tbcMain.SelectTab(tabOverView);
        }

        private void frmmUserMtn_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
        }

        private void dgvListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (BaseFunctions.IsDatagridCell(this.dgvListView, e))
            {
                this.tbcMain.SelectTab(tabDetail);
                this.mFormStatus = FormStatus.Update;
                SetButtonStatus(); 
            }
        }

        private void tbcMain_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPage == tabDetail)
            {
                
                if (mFormStatus == FormStatus.AddNew) 
                    mstrRecID = "";
                else
                    mstrRecID = CvtFunc.ToString(this.dgvListView.CurrentRow.Cells["RecID"].Value);

                if (mFormStatus == FormStatus.Query) mFormStatus = FormStatus.Update;
                FillTextBox();
                SetButtonStatus();
            }
            else if (e.TabPage == tabOverView)
            {
                if (mFormStatus == FormStatus.Update || mFormStatus == FormStatus.AddNew)
                {
                    if (!Update()) e.Cancel = true;
                }
                else
                {
                    mstrRecID = "";                    
                }
                mFormStatus = FormStatus.Query;
                SetButtonStatus();                    
                dgvListView.RefreshData();
            }
            
        }

        private bool Update()
        {
            if (!VaildCheck()) return false ;

            bool blnIsNew = false;
            string strFileds = string.Empty;
            string strSQL = string.Empty;
            strSQL = "select * from " + mstrTableName + " where recid='" + mstrRecID  + "'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow == null)
            {
                drRow = mdbcon.GetDataTable(strSQL).NewRow();
                blnIsNew = true;
            }
            strFileds = DataFunctions.GetUpdateSQL(mstrTableName, "");
            foreach (Control conTxtBox in this.pnlDetail.Controls)
            {
                drRow["RecID"] = CvtFunc.ToInt(mstrRecID );
                foreach (string strField in strFileds.Split(','))
                {
                    if ((conTxtBox.GetType().Name == "TextBox" || conTxtBox.GetType().Name == "ComboBox" ) && GetFieldName(conTxtBox.Name).ToUpper() == strField.ToUpper())
                    {
                        drRow[strField] = conTxtBox.Text;
                    }
                }
            }
            if (blnIsNew == true)
                DataFunctions.UpdateTable(mdbcon, strFileds, mstrTableName, drRow, FormStatus.AddNew);
            else
                DataFunctions.UpdateTable(mdbcon, strFileds, mstrTableName, drRow, FormStatus.Update);


             
            mstrRecID  = CvtFunc.ToString(drRow["RecID"]);
            mFormStatus = FormStatus.Query; 
            return true;
        }

        private bool Delete()
        {
            try
            {
                if (mstrRecID == "") return true;
                string strSQL = string.Empty;
                if (BaseFunctions.SureDelete())
                {
                    strSQL = "Delete from " + this.mstrTableName + " where RecID='" + mstrRecID + "' ";
                    mdbcon.ExecuteSql(strSQL);
                }
                mFormStatus = FormStatus.Query;
                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }

        }

        private void FillTextBox()
        {
            string strSQL = string.Empty;
            string[] strFieldName;
            strFieldName = DataFunctions.GetUpdateSQL(mstrTableName, "").Split(new char[] { ',' });

            strSQL = "select top 1 * from " + mstrTableName + " where RecID='"+ mstrRecID +"'";
            DataRow drRow = mdbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                foreach (string strField in strFieldName)
                {
                    string strValue = strField.Trim();
                    foreach (Control Con in mArrayControl)
                    {
                        if (GetFieldName(Con.Name).ToUpper() == strValue.ToUpper() && ( Con.GetType().Name == "TextBox" || Con.GetType().Name== "ComboBox"    ) )
                        {
                            Con.Text = CvtFunc.ToString(drRow[strValue]);
                        }
                    }
                }                
            }
        }

        private string GetFieldName(string con)
        {
            if (con.Length > 3)
            {
                return con.Substring(3, con.Length - 3);
            }
            return "";
        }

        private bool VaildCheck()
        {
            string strSQL = string.Empty;
            
            if (mFormStatus == FormStatus.AddNew)
            {
                if (txtUserNo.Text == "")
                {
                    MessageBox.Show("用户名不能为空.", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtUserNo.Focus();
                    return false;
                }
                else if (DataFunctions.CheckRecordExist(this.mstrTableName, txtUserNo.Text, "userno"))
                {
                    MessageBox.Show("此用户已经存在.", SysVar.WarnTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtUserNo.Focus();
                    return false;
                }
            }
            else if (mFormStatus == FormStatus.Update)
            {
                return true;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            mFormStatus = FormStatus.AddNew;
            mstrRecID = "";
            SetButtonStatus();
            this.tbcMain.SelectTab(tabDetail);

            txtPassword.Text = "";
            txtUserName.Text = "";
            txtUserNo.Text = "";
            
        }

        private void SetButtonStatus()
        {
            if (mFormStatus == FormStatus.AddNew)
            {
                btnNew.Enabled = false;
                btnDel.Enabled = false;
                btnSave.Enabled = true;
            }
            else if (mFormStatus == FormStatus.Update)
            {
                btnSave.Enabled = true;
                btnNew.Enabled = true;
                btnDel.Enabled = true;
            }
            else if (mFormStatus == FormStatus.Query)
            {
                btnSave.Enabled = false;
                btnNew.Enabled = true;
                btnDel.Enabled = false;
            }
            SetControl();
        }

        private void SetControl()
        {
            if (mFormStatus == FormStatus.Update)
            {
                txtUserName.ReadOnly = true;
                txtUserNo.ReadOnly = true;
            }
            else if (mFormStatus == FormStatus.AddNew)
            {
                txtUserNo.ReadOnly = false; 
                txtUserName.ReadOnly = false;
                //txtGroupNo.Text = "";
                txtUserName.Text ="";
            }
            else
            {
                txtUserName.ReadOnly = true;
                txtUserNo.ReadOnly = true;
            }
            if (SysVar.UserName.ToUpper().Trim()  == txtUserNo.Text.ToUpper().Trim() )
            {
                lblPassword.Visible = true ;
                txtPassword.Visible = true ;
            }
            else
            {
                lblPassword.Visible = false;
                txtPassword.Visible = false;
            }
        }

        private void dgvListView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (this.dgvListView.Rows[e.RowIndex] != null)
            {
                mstrRecID = CvtFunc.ToString(this.dgvListView.Rows[e.RowIndex].Cells["RecID"].Value);
                FillTextBox();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (Delete())
                this.tbcMain.SelectTab(tabOverView);
        }
    }
}
